<?php

class SiteController extends Controller
{
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		$newbooks = UrBook::model()->findAll(array(
			'order'=>'publish_datetime',
			'limit'=>10,
		));

		$hotbooks = UrVote::model()->with('book')->findAll(array(
			'select'=>'*,sum(desire) as desirelevel',
			'group'=>'bid',
			'order'=>'desirelevel desc',
			'limit'=>10
		));

		//var_dump($hotbooks[0]->book->attributes);exit;

		$this->render('index',array('newbooks'=>$newbooks,'hotbooks'=>$hotbooks));
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
	    if($error=Yii::app()->errorHandler->error)
	    {
	    	if(Yii::app()->request->isAjaxRequest)
	    		echo $error['message'];
	    	else
	        	$this->render('error', $error);
	    }
	}


	/**
	 * Displays the contact page
	 */
	public function actionContact()
	{
		$model=new ContactForm;
		if(isset($_POST['ContactForm']))
		{
			$model->attributes=$_POST['ContactForm'];
			if($model->validate())
			{
				$headers="From: {$model->email}\r\nReply-To: {$model->email}";
				mail(Yii::app()->params['adminEmail'],$model->subject,$model->body,$headers);
				Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
				$this->refresh();
			}
		}
		$this->render('contact',array('model'=>$model));
	}

	/**
	 * Displays the login page
	 */
	public function actionLogin()
	{
		$model=new LoginForm;

		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['LoginForm']))
		{
			$model->attributes=$_POST['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login())
				$this->redirect(Yii::app()->user->returnUrl);
		}
		// display the login form
		$this->render('login',array('model'=>$model));
	}

	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}

	/**
	 * import the category
	 */
	 public function actionImportcategory(){
		 $file = bp().'/extensions/bookcategory.txt';
		 $handle = fopen($file,'r');
		 while(!feof($handle)){
			 $buffer[] = fgets($handle,4096);
			 }
			 array_pop($buffer);
		foreach($buffer as $name){
			$category = new UrBookCategory;
			$category->name = $name;
			if($category->save()){
				echo $name.'保存成功<br/>';
				}
			}
		 }


	/**
	 * 从temp数据库导入图书
	 */

	 public function actionImportBook(){
		 $temp = Temp::model()->findAll('id<?',array(101));
		 foreach($temp as $book){
			 echo '####BEGIN###########';
			 $urbook = new UrBook;
			 $urbookextra = new UrBookExtra;
			 $urbook->name = $book['bookname'];
			 $urbook->author = $book['author'];
			 $urbook->publish = $book['publish'];
			$urbook->format  = $book['kaiben'];
			$urbook->package = $book['zhuangzhen'];
			$urbook->market_price = $book['unitprice'];
			 $arrCategoryName = array(
				'A'=>'马克思主义、列宁主义、毛泽东思想、邓小平理论',
				'B'=>'哲学、宗教',
				'C'=>'社会科学总论',
				'D'=>'政治、法律',
				'E'=>'军事',
				'F'=>'经济',
				'G'=>'文化、科学、教育、体育',
				'H'=>'语言、文字',
				'I'=>'文学',
				'J'=>'艺术',
				'K'=>'历史、地理',
				'N'=>'自然科学总论',
				'O'=>'数理科学和化学',
				'P'=>'天文学、地球科学',
				'Q'=>'生物科学',
				'R'=>'R医药、卫生',
				'S'=>'农业科学',
				'T'=>'工业技术',
				'U'=>'交通运输',
				'V'=>'航空、航天',
				'X'=>'环境科学、安全科学',
				'Z'=>'综合性图书'
			 );
			 $arrCategoryId = array(
				'A'=>1,
				'B'=>2,
				'C'=>3,
				'D'=>4,
				'E'=>5,
				'F'=>6,
				'G'=>7,
				'H'=>8,
				'I'=>9,
				'J'=>10,
				'K'=>11,
				'N'=>12,
				'O'=>13,
				'P'=>14,
				'Q'=>15,
				'R'=>16,
				'S'=>17,
				'T'=>18,
				'U'=>19,
				'V'=>20,
				'X'=>21,
				'Z'=>22
			 );
			 if(isset($book['fenlei'])){
				$temp_id = substr($book['fenlei'],0,1);
				$urbook->categoryId = $arrCategoryId[$temp_id];
			 }
			 var_dump($urbook->save());
			 if($urbook->validate()){
				 $urbookextra->id = $urbook->id;
				 $urbookextra->book_intro = $book['jianjie'];
				 if($urbookextra->save()){
					 echo 'Id:'.$urbook->id.' success!<br/>';
					 }
			}
		 }
	 }


}
